package com.simas.common;

import java.io.IOException;

//import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.servicebook.ServiceBook;
import net.rim.device.api.servicebook.ServiceRecord;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.WLANInfo;

public class CommonConnection {
	
	private ServiceRecord getWAP2ServiceRecord() {
		ServiceBook sb = ServiceBook.getSB();
		ServiceRecord[] records = sb.getRecords();
		for(int i = 0; i < records.length; i++) {
			String cid = records[i].getCid().toLowerCase();
			String uid = records[i].getUid().toLowerCase();
			if (cid.indexOf("wptcp") != -1 &&
					uid.indexOf("wifi") == -1 &&
					uid.indexOf("mms") == -1) {
					return records[i];
			}
		}
		return null;
	}
	
	public HttpConnection getConnection(String strUrl) {
		HttpConnection con = null;
		try {
			// pakai ini untuk uji coba di simulator dan smart
			// tanpa connection parameters
			String connectionParameters = "";
			if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
				// Connected to a WiFi access point
				System.out.println("Connected to a WiFi access point");
				connectionParameters = ";interface=wifi";
			} else {
				int coverageStatus = CoverageInfo.getCoverageStatus();
				ServiceRecord record = getWAP2ServiceRecord();
				if (record != null && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
					// Have network coverage and a WAP 2.0 service book record
					System.out.println("Have network coverage and a WAP 2.0 service book record");
					connectionParameters = ";deviceside=true;ConnectionUID=" + record.getUid();
				} else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
					// Have an MDS service book and network coverage
					System.out.println("Have an MDS service book and network coverage");
					connectionParameters = ";deviceside=false";
				} else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
					// Have network coverage but no WAP 2.0 service book record
					System.out.println("Have network coverage but no WAP 2.0 service book record");
					connectionParameters = ";deviceside=true";
				}
			}
			
			con = (HttpConnection)Connector.open(strUrl + connectionParameters);
//			con = (HttpConnection)Connector.open(strUrl);
			
			// belum teruji...
//			TransportDetective td = new TransportDetective();
//			if( td.isCoverageAvailable(TransportDetective.DEFAULT_TCP_CELLULAR) ){
//			   URLFactory urlFactory = new URLFactory(strUrl);
//			   ServiceRecord defaultTCPSR = td.getDefaultTcpCellularServiceRecord();
//			   String connectionURL = urlFactory.getHttpDefaultTcpCellularUrl(defaultTCPSR);
//			   con = (HttpConnection) Connector.open(connectionURL);
//			} else if( td.isCoverageAvailable(TransportDetective.TRANSPORT_BIS_B) ){
//			   URLFactory urlFactory = new URLFactory(strUrl);
//			   String connectionURL = urlFactory.getHttpBisUrl();
//			   con = (HttpConnection) Connector.open(connectionURL);
//			} else if( td.isCoverageAvailable(TransportDetective.TRANSPORT_MDS) ){
//			   URLFactory urlFactory = new URLFactory(strUrl);
//			   String connectionURL = urlFactory.getHttpMdsUrl(true);
//			   con = (HttpConnection) Connector.open(connectionURL);
//			} else if( td.isCoverageAvailable(TransportDetective.TRANSPORT_TCP_CELLULAR) ){
//			   URLFactory urlFactory = new URLFactory(strUrl);
//			   String apn = "";
//			   String apnUser = "";
//			   String apnPassword = "";
//			   String connectionURL = urlFactory.getHttpTcpCellularUrl(apn, apnUser, apnPassword);
//			   con = (HttpConnection) Connector.open(connectionURL);
//			} else if( td.isCoverageAvailable(TransportDetective.TRANSPORT_TCP_WIFI) ){
//			   URLFactory urlFactory = new URLFactory(strUrl);
//			   String connectionURL = urlFactory.getHttpTcpWiFiUrl();
//			   con = (HttpConnection) Connector.open(connectionURL);
//			} else if( td.isCoverageAvailable(TransportDetective.TRANSPORT_WAP) ){
//			   URLFactory urlFactory = new URLFactory(strUrl);
//			   String gatewayAPN = "";
//			   String gatewayIP = "";
//			   String gatewayPort = "";
//			   String sourceIP = "";
//			   String sourcePort = "";
//			   String username = "";
//			   String password = "";
//			   boolean enableWTLS = false;
//			   String connectionURL = urlFactory.getHttpWap1Url(gatewayAPN, gatewayIP, gatewayPort, sourceIP, sourcePort, username, password, enableWTLS);
//			   con = (HttpConnection) Connector.open(connectionURL);
//			} else if( td.isCoverageAvailable(TransportDetective.TRANSPORT_WAP2) ){
//			   URLFactory urlFactory = new URLFactory(strUrl);
//			   ServiceRecord wap2ServiceRecord = getWAP2ServiceRecord();
//			   String connectionURL = urlFactory.getHttpWap2Url(wap2ServiceRecord);
//			   con = (HttpConnection) Connector.open(connectionURL);
//			}
		}
		catch (IOException ex) {
			
		}
		return con;
	}
	
}
